package com.asktgapp.adapter;
/*
 * 项目名:     JulyProject
 * 包名:       com.zsy.words.adapter
 * 文件名:     MyAdapter
 * 创建者:     阿钟
 * 创建时间:   2016/11/17 19:22
 * 描述:       ListView列表适配器
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.model.BrandVO;
import com.asktgapp.model.Person;
import com.asktgapp.test.BrandListVO;
import com.asktgapp.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

public class MyBrandAdapter extends BaseAdapter {
    private List<BrandVO> list;
    private LayoutInflater inflater;
    private List<BrandListVO> mBrand = new ArrayList<>();
    private List<BrandListVO> mBrandAdapter = new ArrayList<>();
    private ItemCallback itemCallback;
    private String[] word = new String[]{"热门品牌", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    private String[] wordUper = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    public MyBrandAdapter(Context context, List<BrandVO> list, ItemCallback itemCallback) {
        inflater = LayoutInflater.from(context);
        this.list = list;
        this.itemCallback = itemCallback;
        //新建26个对象，对应26个字母的集合
        for (int i = 0; i < 27; i++) {
            BrandListVO vo = new BrandListVO();
            vo.setHeadChar(word[i]);
            mBrand.add(vo);
        }
        //按首字母将各个品牌分类
        for (int i = 0; i < this.list.size(); i++) {
            if (this.list.get(i).getIsHot() == 1) {//筛选出热门品牌
                mBrand.get(0).addlist(this.list.get(i));
            }
            for (int j = 1; j < mBrand.size(); j++) {
                if (this.list.get(i).getHeaderWord().equals(mBrand.get(j).getHeadChar().toUpperCase())) {
                    mBrand.get(j).addlist(this.list.get(i));
                }
            }
        }
        for (int i = 0; i < mBrand.size(); i++) {
            if (mBrand.get(i).getList() != null && mBrand.get(i).getList().size() > 0) {
                mBrandAdapter.add(mBrand.get(i));
            }
        }
    }

    @Override
    public int getCount() {
        return mBrandAdapter.size();
    }

    @Override
    public Object getItem(int position) {
        return mBrandAdapter.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_brand, null);
            holder.headChar = (TextView) convertView.findViewById(R.id.tv_head_char);
            holder.mFlowLayout = (FlowLayout) convertView.findViewById(R.id.flowLayout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String word = mBrandAdapter.get(position).getHeadChar();
        final List<BrandVO> brand = mBrandAdapter.get(position).getList();
        holder.mFlowLayout.removeAllViews();

        for (int i = 0; i < brand.size(); i++) {
            View view = null;
            view = inflater.inflate(R.layout.item_brand_lable, holder.mFlowLayout, false);
            FrameLayout scope = view.findViewById(R.id.container);
            TextView textView = view.findViewById(R.id.lable);

            textView.setText(brand.get(i).getName());
            final int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可在接口中添加参数，传递需要的数据
                    itemCallback.onItemClick(brand.get(finalI).getName(), brand.get(finalI).getId());
                }
            });
            holder.mFlowLayout.addView(view);
        }
        holder.headChar.setText(word.toUpperCase());


        return convertView;

    }

    private class ViewHolder {
        private TextView headChar;
        private FlowLayout mFlowLayout;


    }

    public interface ItemCallback {
        void onItemClick(String string, String id);
    }

    public List<BrandListVO> getData() {
        return mBrandAdapter;
    }
}
